package cn.com.spic.demo.controller;

import cn.com.spic.demo.api.ActionProjectService;
import cn.com.spic.demo.api.ProgressReportService;
import cn.com.spic.demo.api.SpecialProjectService;
import cn.com.spic.demo.dto.ActionProejctDTO;
import cn.com.spic.demo.dto.SpecialComRateDTO;
import cn.com.spic.demo.dto.SpecialProjectDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 九大专项
 */
@RestController
@RequestMapping(value = "/special")
public class SpecialProController {

    @Autowired
    private SpecialProjectService specialProjectService;
    @Autowired
    private ActionProjectService actionProjectService;

    @Autowired
    private ProgressReportService progressReportService;
    @GetMapping(value ="/getAllSpecialPro")
    public String  getSpecialProDto(){
        JSONArray array=new JSONArray();
        try{
            DecimalFormat df=new DecimalFormat("0.000");
            List<SpecialProjectDTO> specialProjectDTOS = specialProjectService.getAllSpecialProList();

            for(SpecialProjectDTO dto:specialProjectDTOS){
                Double allActualRate = 0.0;
                List<ActionProejctDTO> dtos = actionProjectService.getActionProDtosByNineId(dto.getId());
                List<SpecialComRateDTO> specialComRateDTOS = actionProjectService.selectSpecialComRate(dto.getId());
                for (SpecialComRateDTO dto1:specialComRateDTOS){
                    List<ActionProejctDTO> actionProejctDTOS = actionProjectService.getActionProDtos(dto1.getParentActionId());
                    double weight = 0.0;
                    if(actionProejctDTOS.size()>0 && actionProejctDTOS.get(0).getWeight() != null){
                        if(dto1.getIdentity()!=null && dto1.getIdentity().equals("1")){
                            weight = Double.parseDouble(actionProejctDTOS.get(0).getWeight());
                        }else {
                            weight = 1-Double.parseDouble(actionProejctDTOS.get(0).getWeight());
                        }
                    }
                    double a = Double.parseDouble(dto1.getSumActualComRate())*weight/Double.parseDouble(dto1.getNumber());
                    allActualRate = allActualRate+a/dtos.size();
                }
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("id",dto.getId());
                jsonObject.put("pid",dto.getId());
                jsonObject.put("name",dto.getSpecialProjectName());
                jsonObject.put("number",actionProjectService.countByExample(dto.getId(),"1"));
                jsonObject.put("allActualRate",df.format(allActualRate));
                jsonObject.put("actions",this.getActionsBySpecialId(dto.getId()));
                array.add(jsonObject);

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return array.toJSONString();
    }

    public List<ActionProejctDTO> getActionsBySpecialId(String specialId) throws ParseException {
        DecimalFormat df=new DecimalFormat("0.00");
        Calendar calendar = Calendar.getInstance();
        //九大项下有多少个行动项数量
        List<ActionProejctDTO> dtos = actionProjectService.getActionProDtosByNineId(specialId);
        for (ActionProejctDTO dto : dtos) {

            List<ActionProejctDTO> dtosLevel2 = actionProjectService.getActionDetailAndLevelList(dto.getId(),"2");
            double allActualRate = 0.0;
            for(ActionProejctDTO oneDto:dtosLevel2){
                int number1 = progressReportService.countByExample(oneDto.getId(),"1");

                double allDepeRate = 0.0;
                Map<String,String> map = new HashMap<>();
                map.put("def2",oneDto.getId());
                map.put("year",String.valueOf(calendar.get(Calendar.YEAR)));
                map.put("month",String.valueOf(calendar.get(Calendar.MONTH) + 1));
                String s = progressReportService.getSumMaxActRateReportdtos(map);
                Double actualFirstDeptRateSum = 0.00;
                if(s!=null &&  !("").equals(s)){
                    actualFirstDeptRateSum = Double.parseDouble(s);
                }
                int number2 = progressReportService.countByExample(oneDto.getId(),"2");

                Map<String,String> map1 = new HashMap<>();
                map1.put("def2",oneDto.getId());
                map1.put("def3","2");
                String s1 = progressReportService.getSumMaxActRateReportdtos(map1);
                Double actualSecDeptRateSum = 0.00;
                if(s1!=null &&  !("").equals(s1)){
                    actualSecDeptRateSum = Double.parseDouble(s1);
                }
                if(oneDto.getWeight()!=null && number1!=0 && number2 !=0){
                    allDepeRate = actualFirstDeptRateSum*Double.parseDouble(oneDto.getWeight())/number1+actualSecDeptRateSum*(1-Double.parseDouble(oneDto.getWeight()))/number2;
                }else {
                    allDepeRate = 0.0;
                }

                allActualRate = allActualRate+allDepeRate;
            }
            dto.setTrueactualComRate(df.format(allActualRate));

        }
        return dtos;
    }
    @GetMapping(value ="/getAllSpecialProList")
    public List<SpecialProjectDTO> getAllSpecialProList(String specialId) throws ParseException {
        List<SpecialProjectDTO> specialProjectDTOS = specialProjectService.getAllSpecialProList();
        return specialProjectDTOS;
    }
    @GetMapping("/getOneSpecialPro")
    public JSONObject getOneSpecialPro(String id){
        SpecialProjectDTO specialProjectDTOS = null;
        JSONObject jsonObject = new JSONObject();
        try{
             specialProjectDTOS = specialProjectService.getOneSpecialPro(id);
             if(specialProjectDTOS !=null){
                 jsonObject.put("startTime",specialProjectDTOS.getStartTime());
                 jsonObject.put("specialProjectName",specialProjectDTOS.getSpecialProjectName());
                 jsonObject.put("endTime",specialProjectDTOS.getEndTime());
                 jsonObject.put("id",specialProjectDTOS.getId());
             }
        }catch(Exception e){
              e.printStackTrace();
        }
        return jsonObject;
    }
}
